﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;

namespace Freshome.m.vms
{
    public abstract class TabItemate : TabItem, INotifyPropertyChanged
    {
        private vws.ClosableTop _top = new vws.ClosableTop();
        private bool _vb;
        public TabItemate(string tabTitle, bool visible)
        {
            _top.DataContext = this;
            if (visible)
            {
                _top.tabutton.Click += new RoutedEventHandler(tabutton_Click);
            }
            else
            {
                _top.tabrief.HorizontalAlignment = HorizontalAlignment.Center;
            }
            _vb = visible; 
            this.Header = _top;
            this.Name = tabTitle;
        }
        public virtual void Loadata() { }

        public event EventHandler CloseRequested;
        void tabutton_Click(object sender, RoutedEventArgs e)
        {
            EventHandler handler = this.CloseRequested;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        protected override void OnSelected(RoutedEventArgs e)
        {
            base.OnSelected(e);
            _top.tabutton.Visibility = _vb ? Visibility.Visible : Visibility.Hidden;
        }
        protected override void OnUnselected(RoutedEventArgs e)
        {
            base.OnUnselected(e);
            _top.tabutton.Visibility = Visibility.Hidden;
        }
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            _top.tabutton.Visibility = _vb ? Visibility.Visible : Visibility.Hidden;
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            if (!this.IsSelected)
            {
                _top.tabutton.Visibility = Visibility.Hidden;
            }
            else
            {
                _top.tabutton.Visibility = _vb ? Visibility.Visible : Visibility.Hidden;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
